﻿-- =============================================
-- Author:		M.A.
-- Create date: 05.08.2013
-- Description:	delete a driver record
-- =============================================
CREATE PROCEDURE [trUser].[sp_Drivers_DELETE] 
	@IDDriver bigint
	
AS
BEGIN
	BEGIN TRANSACTION
		BEGIN TRY
			
			-- update a driver record based on ID
			UPDATE tblDrivers	
				SET DateInactive = GETDATE()
			WHERE IDDriver=@IDDriver
	 	
			-- return the new Id and the error (0) info
			SELECT 
				 @IDDriver AS IDDriver
			    ,0 as IDUser
				,0 AS ErrorNumber
				,'' AS ErrorMessage; 				
		END TRY
		BEGIN CATCH		-- Error happens
				SELECT 
					-1 AS IDDriver
					,- 1 as IDUser
					,ERROR_NUMBER() AS ErrorNumber
					,ERROR_MESSAGE() AS ErrorMessage;
		
			-- rollback transaction
			IF @@TRANCOUNT > 0
				ROLLBACK TRANSACTION;
				
		END CATCH;
		
	-- commit transaction	
	IF @@TRANCOUNT > 0
		COMMIT TRANSACTION;						
END

-- TEST HERE
	/* 
		EXEC [sp_Drivers_DELETE] @IDUser=12
	*/